package sm.test;

import java.sql.SQLException;
import java.util.Date;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.codec.type.Logical;
import sm.def.Type;
import sm.upgrade.util.UpgradeContext;
import sm.util.SMMap;
import sm.util.Util;

public class HibernateTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		UpgradeContext context = UpgradeContext.getInstance();
		ApplicationContext customerContext = context
				.getOrCreateUpgradeContext();

		HibernateTemplate template = (HibernateTemplate) customerContext
				.getBean("template");
		template.execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Transaction tr = session.beginTransaction();
				SMMap test = new SMMap();
				test.put("test1", 1d);
				test.put("test2", "test2");
				String dateStr = "-3 00:00:00";
				Object date = Util.parse(dateStr, Type.TIME_TYPE);
				
				test.put("test3", date);
				test.put("test4", Logical.FALSE);
				test.put("test5", "");
				test.put("test6", "");
				test.put("test7", "");
				session.save("test", test);
				tr.commit();
				
				/*List<Object> tests = session.createQuery("from test").list();
				Object test = tests.get(0);
				Gson gson = new Gson();
				System.out.println(gson.toJson(test));*/

				return null;
			}
		});

	}
}
